package com.ng.framework.report;

import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.util.JRLoader;

public abstract class NGReport<T> implements JRDataSource {

	private List<T> objects;
	protected int index = -1;
	private JasperReport report;

	public abstract String getReportPath();
	public abstract Map<String, Object> getParameters();

	public final JasperReport getReport() {
		return this.report;
	}
	public final JasperPrint getJasperPrint() {
		try {
			return JasperFillManager.fillReport(getReport(), getParameters(), this);
		} catch (JRException e) {
			e.printStackTrace();
		}
		return null;
	}

	public final void buildReport() throws JRException {
		report = (JasperReport) JRLoader.loadObject(getReportPath());
	}
	@Override
	public final boolean next() throws JRException {
		return ++index < objects.size();
	}
	public final List<T> getObjects() {
		return objects;
	}
	public final void setObjects(List<T> objects) {
		this.objects = objects;
	}

	public byte[] getPDF() {
		try {
			return JasperExportManager.exportReportToPdf(getJasperPrint());
		} catch (JRException e) {
			e.printStackTrace();
		}
		return null;
	}

}
